package com.team.hiring.company.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.team.hiring.common.entity.ApplicationEntity;
import com.team.hiring.common.util.DBUtil;
import com.team.hiring.company.entity.CompanyEntity;
import com.team.hiring.talent.entity.ResumeEntity;
import com.team.hiring.talent.entity.TalentEntity;

public class CompanyDao {
	
	/**
	 * 企业登录
	 * @return
	 */
	public CompanyEntity goCompanyMain(String code, String password) {
		CompanyEntity companyEntity = new CompanyEntity();
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			 String sql = "SELECT c.id,c.name,c.code,c.password FROM company c WHERE c.code=? AND c.password=?";
			 prepareStatement = connection.prepareStatement(sql);
			 prepareStatement.setString(1, code);
			 prepareStatement.setString(2, password);
			 resultSet = prepareStatement.executeQuery();
			 while (resultSet.next()){
				 companyEntity.setId(resultSet.getInt("c.id"));
				 companyEntity.setName(resultSet.getString("c.name"));
				 companyEntity.setCode(resultSet.getString("c.code"));
				 companyEntity.setPassword(resultSet.getString("c.password"));
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(resultSet, prepareStatement, connection);
		}
		return companyEntity;
	}

	
	/**
	 * 企业注册
	 * @param companyEntity
	 * @return
	 */
	public boolean register(CompanyEntity companyEntity) {
		boolean result = false;
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		try {
			 String sql = "INSERT INTO company VALUES(NULL,?,?,?,?,?,?,?,?,?)";
			 prepareStatement = connection.prepareStatement(sql);
			 prepareStatement.setString(1,companyEntity.getCode());
			 prepareStatement.setString(2,companyEntity.getPassword());
			 prepareStatement.setString(3,companyEntity.getName());
			 prepareStatement.setString(4,companyEntity.getEmail());
			 prepareStatement.setString(5,companyEntity.getPhone());
			 prepareStatement.setString(6,companyEntity.getLinkman());
			 prepareStatement.setString(7,companyEntity.getAddress());
			 prepareStatement.setInt(8,companyEntity.getNumbers());
			 prepareStatement.setString(9,companyEntity.getSlogan());
			 int row = prepareStatement.executeUpdate();
			 if (row !=0){
				 result = true;
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(prepareStatement, connection);
		}
		return result;
	}


	
	/**
	 * 企业详细信息
	 * @return
	 */
	public CompanyEntity gocompanyDetailUI(int companyId) {
				CompanyEntity companyEntity = new CompanyEntity();
				Connection connection = DBUtil.getConnection();
				PreparedStatement prepareStatement = null;
				ResultSet resultSet = null;
				 try {
					 	String sql = "SELECT * FROM company c WHERE c.id=?";
						prepareStatement = connection.prepareStatement(sql);
						prepareStatement.setInt(1,companyId);
						resultSet = prepareStatement.executeQuery();
						if(resultSet.next()){
							companyEntity.setCode(resultSet.getString("code"));
							companyEntity.setPassword(resultSet.getString("password"));
							companyEntity.setName(resultSet.getString("name"));
							companyEntity.setEmail(resultSet.getString("email"));
							companyEntity.setPhone(resultSet.getString("phone"));
							companyEntity.setLinkman(resultSet.getString("linkman"));
							companyEntity.setAddress(resultSet.getString("address"));
							companyEntity.setNumbers(resultSet.getInt("numbers"));
							companyEntity.setSlogan(resultSet.getString("slogan"));
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				return companyEntity;
			}


	/**
	 * 跳转到修改信息界面,并显示以前的信息
	 * @param companyId
	 * @return
	 */
	public CompanyEntity updateMassage(int companyId) {
		return gocompanyDetailUI(companyId);
	}


	
	/**
	 * 执行修改操作
	 * @param companyEntity
	 * @return
	 */
	public boolean updateMassage(CompanyEntity companyEntity) {
		boolean result = false;
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		try {
			 String sql = "UPDATE company set name=?,email=?,phone=?,linkman=?,address=?,numbers=?,slogan=? WHERE id=?";
			 prepareStatement = connection.prepareStatement(sql);
			 prepareStatement.setString(1,companyEntity.getName());
			 prepareStatement.setString(2,companyEntity.getEmail());
			 prepareStatement.setString(3,companyEntity.getPhone());
			 prepareStatement.setString(4,companyEntity.getLinkman());
			 prepareStatement.setString(5,companyEntity.getAddress());
			 prepareStatement.setInt(6,companyEntity.getNumbers());
			 prepareStatement.setString(7,companyEntity.getSlogan());
			 prepareStatement.setInt(8,companyEntity.getId());
			 int row = prepareStatement.executeUpdate();
			 if (row !=0){
				 result = true;
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(prepareStatement, connection);
		}
		return result;
		
	}

	/**
	 * 修改密码
	 * @param newpassword
	 * @return
	 */
	public boolean updatepassword(String newpassword,int id) {
		boolean result = false;
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		try {
			 String sql = "UPDATE company set password=? WHERE id=?";
			 prepareStatement = connection.prepareStatement(sql);
			 prepareStatement.setString(1,newpassword);
			 prepareStatement.setInt(2,id);
			 int row = prepareStatement.executeUpdate();
			 if (row !=0){
				 result = true;
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(prepareStatement, connection);
		}
		return result;
	}


	
	/**
	 * 查询公司详细信息（主页点击公司名称之后）
	 * @param companyId
	 * @return
	 */
	public CompanyEntity homepageCompanyDateilInfo(int companyId) {
		CompanyEntity companyEntity = new CompanyEntity();
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		 try {
			 	String sql = "SELECT * FROM company c WHERE c.id=?";
				prepareStatement = connection.prepareStatement(sql);
				prepareStatement.setInt(1,companyId);
				resultSet = prepareStatement.executeQuery();
				if(resultSet.next()){
					companyEntity.setName(resultSet.getString("name"));
					companyEntity.setEmail(resultSet.getString("email"));
					companyEntity.setPhone(resultSet.getString("phone"));
					companyEntity.setLinkman(resultSet.getString("linkman"));
					companyEntity.setAddress(resultSet.getString("address"));
					companyEntity.setNumbers(resultSet.getInt("numbers"));
					companyEntity.setSlogan(resultSet.getString("slogan"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return companyEntity;
	}

	/**
	 * 公司自己页面查看求职者简历
	 * @param request
	 * @param response
	 */
	public List<TalentEntity> goResumeDetailUI(int talentId) {
		List<TalentEntity> talentEntityList = new ArrayList<TalentEntity>();
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			String sql = "SELECT t.name,t.gender,t.age,r.intention as 'resume$intention',r.work_experience as 'resume$workExperience',"
					+ "r.project_experience as 'resume$projectExperience',t.phone,t.email,t.hobby FROM talent t,resume r WHERE"
					+ " t.id=r.talent_id AND t.id=? ";
					
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setInt(1, talentId);
			resultSet = prepareStatement.executeQuery();
			while (resultSet.next()) {
				TalentEntity talentEntity = new TalentEntity();
				talentEntity.setName(resultSet.getString("t.name"));
				talentEntity.setGender(resultSet.getInt("t.gender"));
				talentEntity.setAge(resultSet.getInt("t.age"));
				talentEntity.setResume$intention(resultSet.getString("resume$intention"));
				talentEntity.setResume$workExperience(resultSet.getString("resume$workExperience"));
				talentEntity.setResume$projectExperience(resultSet.getString("resume$projectExperience"));
				talentEntity.setPhone(resultSet.getString("t.phone"));
				talentEntity.setEmail(resultSet.getString("t.email"));
				talentEntity.setHobby(resultSet.getString("t.hobby"));
				talentEntityList.add(talentEntity);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(resultSet, prepareStatement, connection);
		}
		return talentEntityList;
	}

	/**
	 * 删除求职信息
	 * @param request
	 * @param response
	 */
	public int deleteApplication(int applicationId) {
		// TODO Auto-generated method stub
		return DBUtil.executeUpdate("DELETE FROM application WHERE id=?",applicationId );
	}

	/**
	 * 注册时查询账号是否存在
	 * @param valueCode
	 * @return
	 */
	public int selectCode(String valueCode) {
		int row =0;
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		 try {
			 	String sql = "SELECT * FROM company c WHERE c.code=?";
				prepareStatement = connection.prepareStatement(sql);
				prepareStatement.setString(1,valueCode);
				resultSet = prepareStatement.executeQuery();
				if(resultSet.next()){
					row = 1;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return row;
	}
	
	

	

	
	
//	/**
//	 * 查询旧密码
//	 * @param id
//	 * @return
//	 */
//	public String oldpasswordSelect(int id) {
//		String oldpassword = "";
//		Connection connection = DBUtil.getConnection();
//		PreparedStatement prepareStatement = null;
//		ResultSet resultSet = null;
//			try {
//					String sql = "SELECT password FROM company WHERE id=?";
//					prepareStatement = connection.prepareStatement(sql);
//					prepareStatement.setInt(1,id);
//					resultSet = prepareStatement.executeQuery();
//					if(resultSet.next()){
//						oldpassword = resultSet.getString("password");
//					}
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}finally{
//					DBUtil.close(prepareStatement, connection);
//				}
//		return oldpassword;
//	}
			 
		
	
	
	
	
}
